
import { Component, Label, Node, Sprite, tween, Vec3, _decorator, UIOpacity, UITransform, size, resources, Prefab, instantiate, find, Quat } from "cc";
import { App } from "../App";
import { Singleton } from "../common/Common";
import UtilTool from "./UtilTool";
import { Asset } from "cc";

const {ccclass, property} = _decorator;
@ccclass('LoadPanel')
export class LoadPanel extends Component {
    @property(Node)
    loadNode: Node = null;

    @property(Node)
    bgBlack: Node = null;

    @property(Node)
    desTxt: Node = null;

    @property({ type: Node })
    bgSpr: Node

    onLoad () {
    }

    start () {
    }

    public show(para)
    {
        UtilTool.setScreen(this.node)
        if (para.content) {
            this.desTxt.getComponent(Label).string = para.content
        }
    
        tween(this.bgSpr)
            .by(2.0, { angle: -360 })
            .repeatForever()
            .start()

        this.delayShow(para)
    }

    public delayShow(para)
    {
        this.bgSpr.setScale(0,0,1)
        this.desTxt.setScale(0,0,1)
        this.bgBlack.active = false
        this.loadNode.setPosition(0,para.posy)

        if (para.type == 0) {
            this.scheduleOnce(() => { 
                this.bgSpr.setScale(1,1,1); 
                this.desTxt.setScale(1,1,1) 
                this.bgBlack.active = true
            }, 1.0)
        }
        else if (para.type == 1) {
            this.bgSpr.setScale(1,1,1)
            this.desTxt.setScale(1,1,1)
            this.bgBlack.active = true
        }
        else if (para.type == 2) {
            
        }

        // 超时处理
        this.scheduleOnce(() => { LoadMg.removeLoad() }, 28)
    }
        
}

//  加载菊花
export class loadMg extends Singleton<loadMg>{
    public loadPrefab:Prefab = null
    public loadNode:Node = null
    // private index = 0
    
    private hasLoadList : any = null


    /**
     * 加载菊花 para
     * @param content 提示文本
     * @param parent 父类 
     * @param order 层级
     * @param type 展示类型
     * @param posy y轴位置
    */
    public showLoad(para = null) {
        para = para || {}
        para.content = para.content || App.language.getLanguageLab(10)
        para.parent = para.parent || find('Canvas')
        para.order = para.order || 100
        para.type = para.type || 0
        para.posy = para.posy || 0

        if(this.loadNode)
        {
            find('Canvas').removeChild(this.loadNode)
            this.loadNode=null;
        }

        this.loadNode = instantiate(this.loadPrefab)
        find('Canvas').addChild(this.loadNode)
        this.loadNode.getComponent(LoadPanel).show(para)
        this.loadNode.setSiblingIndex(100)
    }

    /**
     * 移除菊花
    */
    public removeLoad() {
        if(this.loadNode)
        {
            find('Canvas').removeChild(this.loadNode)
            this.loadNode=null;
        }

        // if (bFin) {
        //     this.index = 0
        // }
        // else {
        //     this.index = this.index - 1
        //     if (this.index > 0) {
        //         return
        //     }
        //     else if (this.index < 0){
        //         this.index = 0
        //     }
        // }

        // if (this.pLoadPanel) {
        //     this.pLoadPanel.visible = false
        // }
    }

    // 加载资源列表
    public loadResList(resList : {path:string, type:typeof Asset, isDir : boolean}[], cb : Function) {  

        this.hasLoadList = {}

        let index = 0

        let done = (path, isDir, res) => {
            index = index + 1

            let resDict = {}
            if (isDir) {
                res.forEach(item => {
                    resDict[item.name] = item
                })
                this.hasLoadList[path] = resDict
            } else {
                this.hasLoadList[path] = res
            }

            if (index == resList.length) {
                cb && cb(this.hasLoadList)
            }
        }

        resList.forEach(item => {
            let path = item.path
            let type = item.type
            let isDir = item.isDir

            if(isDir) {
                resources.loadDir(path, type, (err, res) => {
                    if (err) {
                        console.error("load dir error", err)
                        return
                    }
    
                    done(path, isDir, res)
                })
            } else {
                resources.load(path, type, (err, res) => {
                    if (err) {
                        console.error("load res error", err)
                        return
                    }
    
                    done(path, isDir, res)
                })
            }
        })

    }
}

export const LoadMg: loadMg = loadMg.getInstance<loadMg>(loadMg);